# 不执行逻辑，目的是为了不让 from apps.crawler_platform.core_callback import *
# 被编辑器认为没有使用，快捷清除时就不会被清除

from re_common.baselibrary.utils.core.mdeprecated import try_except2_async

from apps.crawler_platform.core_callback import null
from apps.crawler_platform.core_callback.core_callback import sql_model_callback
from apps.crawler_platform.core_callback.err_callback import all_client_back_callback
from apps.crawler_platform.core_platform.core_g import SQLTable
from apps.crawler_platform.core_platform.core_nosql import NoSqlUtil
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from apps.crawler_platform.core_platform.g_model import DownModel, HtmlModel, OtherListModel
from apps.crawler_platform.other_platform.base_other_step import BaseOtherStep, BaseOtherItems

STATICA_HACK = False
# globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:  # pragma: no cover
    null()


class ListParaItems(BaseOtherItems):

    def __init__(self):
        super(ListParaItems, self).__init__()
        self.page_index = 1

    def get_pkm(self):
        """
        获取主键
        :return:
        """
        return {"task_name": self.task_name,
                "task_tag": self.task_tag,
                "list_rawid": self.list_rawid,
                "page_index": self.page_index}

    def set_downmodel(self):
        self.down_model = DownModel[HtmlModel](down_dict={})


class OtherDownStep(BaseOtherStep):

    def __init__(self, request, inputs):
        super().__init__()
        self.err_tag = "otherlist"
        self.pm = ListParaItems()
        self.pm.set_downmodel()
        # api传入数据
        self.input = inputs
        self.pm.task_name = inputs.data.task_name
        self.pm.task_tag = inputs.data.task_tag
        self.pm.list_rawid = inputs.data.list_rawid
        self.pm.page_index = inputs.data.page_index
        self.pm.Id = "_".join([self.pm.list_rawid, str(self.pm.page_index)])

        # fastapi 自带的 Request
        self.request = request
        self.pm.redis = request.app.state.redis
        # 每一步的文字说明
        self.step_info = ""
        # 解析字段字典
        self.para_dicts = {}

        # 读取sql的model
        self.sql_model: OtherListModel

        self.task_table = SQLTable.other_list

        self.sql_next_table = SQLTable.other_article

    def set_table(self, *args, **kwargs):
        self.sql_model = OtherListModel
        if self.pm.task_tag in ["isticword_list", "cnkipaperlist", "cnkithesislist", "cnkicdfdthesislist",
                                "cnkiconferencelist", "cnkiipfdconferencelist", "wanfangthesis_search_list",
                                "wanfangconference_search_list", "baiduxuelist"]:
            self.sql_next_table = SQLTable.other_sub_class
        return self.task_table

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def deal_html(self):
        return await super().deal_html(OtherListModel)

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def save_sql(self, type="", code=""):
        """
        与关系型sql的处理 包括存储下一级的sql以及本级sql的状态和字段更新
        """
        bools, msg = await super().save_sql(self.task_table, self.sql_next_table, type, code)
        return bools, msg

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def get_sql(self):
        row = await CoreSqlMixin.get_otherlist(self.pm.task_name, self.pm.task_tag, self.pm.list_rawid,
                                               self.pm.page_index)
        if row:
            row = sql_model_callback(row)
            self.pm.sql_model = OtherListModel.parse_obj(row)
            return True, row
        else:
            return False, {
                "err_msg": "qk list sql查询不存在;" + self.pm.task_tag + "," + self.pm.list_rawid + ',' + str(
                    self.pm.page_index)}

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def save_nosql_html(self):
        # 对于期刊级，只要能走到这一步都认为下载完成
        NoSqlUtil.set_is_his(self.pm, "True")
        bools, dicts = await super().save_nosql_html()
        return bools, dicts
